package com.jz.yanshe.ui.widget;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewConfiguration;

/**
 * Created by Administrator on 2018/4/10.
 */

public class MyRecyclerView extends RecyclerView {

    float x, mLastY,y, newX, newY;
    private int mTouchSlop;
    private boolean flag = false;

    public MyRecyclerView(Context context) {
        super(context);
        init(context);
    }

    public MyRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public MyRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    private void init(Context context) {
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
//        System.out.println("dispatchTouchEvent      aaaaaaaaaaaaaaaa");
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            y = ev.getY();
            x = ev.getX();
        }
        if (ev.getAction() == MotionEvent.ACTION_MOVE) {
            if ((y - ev.getY() < 0 && !this.canScrollVertically(-1)) || (y - ev.getY() > 0 && !this.canScrollVertically(1))) {
                return false;
            } else {
                return super.dispatchTouchEvent(ev);
            }
        }
        if (ev.getAction() == MotionEvent.ACTION_UP) {
            newY = ev.getY();
            newX = ev.getX();
        }
//        if(this.canScrollVertically(1) && this.canScrollVertically(-1)){
//            return super.dispatchTouchEvent(ev);
//        }else{
//            return false;
//        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent e) {
//        System.out.println("onTouchEvent  MyRecyclerView");
        return super.onTouchEvent(e);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
//        System.out.println("onTouchEvent  onInterceptTouchEvent");
////        System.out.println(x - newX);
////        System.out.println(y - newY);
//        if (flag) {
//            flag = false;
//            return true;
//        }
//        int action = ev.getAction();
//        float y = ev.getY();
//
//        switch (action) {
//            case MotionEvent.ACTION_DOWN:
//                mLastY = y;
//                break;
//
//            case MotionEvent.ACTION_MOVE:
//                float dy = y - mLastY;
//                System.out.println(Math.abs(dy) + "   Math.abs(dy)");
//                System.out.println(mTouchSlop + "  mTouchSlop");
//                if (Math.abs(dy) > mTouchSlop) {
//                    System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaa");
//                    ev.setAction(MotionEvent.ACTION_DOWN);
//                    flag = true;
//                    onInterceptTouchEvent(ev);
//                    return true;
//                } else {
//                    System.out.println("bbbbbbbbbbbbbbbbbbbbbbbb");
//                    return super.onInterceptTouchEvent(ev);
//                }
//        }
        return super.onInterceptTouchEvent(ev);
    }
}
